package com.xq.shop.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xq.shop.dao.domain.entity.OperationLog;
import com.xq.shop.dao.mapper.OperationLogMapper;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.service.IOperationLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.vo.OperationLogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import utils.BaseConstants;
import utils.BasePage;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-16
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {


    @Override
    public BasePage<OperationLogVo> pageQuery(QueryDto dto) {
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getContent())) {
            wrapper.like(OperationLog::getUsername, dto.getContent()).or().like(OperationLog::getRequestUrl, dto.getContent())
                    .or().like(OperationLog::getContent, dto.getContent());
        }
        wrapper.orderByDesc(OperationLog::getId);
        IPage<OperationLog> page = page(new Page<>(dto.getPage(), dto.getLimit()), wrapper);
        List<OperationLog> loginLogList = page.getRecords();
        List<OperationLogVo> voList = beanToVo(loginLogList);
        return new BasePage<OperationLogVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), voList);
    }

    private List<OperationLogVo> beanToVo(List<OperationLog> loginLogList) {
        List<OperationLogVo> voList = new ArrayList<>();
        loginLogList.forEach(loginLog -> {
            OperationLogVo vo = new OperationLogVo();
            BeanUtils.copyProperties(loginLog, vo);
            vo.setOperationTime(BaseConstants.SDF2.format(loginLog.getOperationTime()));
            voList.add(vo);
        });
        return voList;
    }

}
